package com.lxb.stock;

import java.io.*;
import java.net.*;


public class SStockService 
{
	String baseUrl = "http://hq.sinajs.cn/list=";
	
	public SStockInfo queryStock(String stockId)
	{
		StringBuffer sb = new StringBuffer(baseUrl);
		if (stockId.equals("szzs"))
			sb.append("sh000001");
		else if (stockId.equals("szcz"))
			sb.append("sz399001");
		else if (stockId.startsWith("0"))
			sb.append("sz").append(stockId);
		else if (stockId.startsWith("6"))
			sb.append("sh").append(stockId);
		
		try
		{
			URL url = new URL(sb.toString());
			HttpURLConnection huc = (HttpURLConnection)url.openConnection();
			huc.setDoInput(true);
			huc.setUseCaches(false);
			huc.setRequestMethod("GET");
			int code = huc.getResponseCode( );         
			String response = huc.getResponseMessage( );
			if (code != 200)
				return null;
		    //System.out.println("HTTP/1.x " + code + " " + response); 

			BufferedInputStream bis = new BufferedInputStream(huc.getInputStream());
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte[] buf = new byte[1000];
			int readed = 0;
			while ((readed = bis.read(buf, 0, 1000)) != -1)
				baos.write(buf, 0, readed);
			String data = new String(baos.toByteArray(), "US-ASCII");
			//System.out.println(data);
			return new SStockInfo(data, stockId);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	    
			
		return null;
	}

}
